package StudyCode.Component;

import StudyCode.UI.ActionFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

/**
 * @author lixiaoyao
 */
public class RobotTest {
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				// make frame with a button panel
				ActionFrame frame = new ActionFrame();
				frame.setTitle("ButtonTest");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});

		// attach a robot the screen device

		GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment().getLocalGraphicsEnvironment();
		GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();

		try {
			final Robot robot = new Robot(graphicsDevice);
			robot.waitForIdle();
			new Thread() {
				public void run() {
					runTest(robot);
				}
			}.start();
		} catch (AWTException ex) {
			ex.printStackTrace();
		}
	}

	public static void runTest(Robot robot) {
		// simulate a space bar press
		robot.keyPress(' ');
		robot.keyRelease(' ');

		// simulate a tab key followed by a space
		robot.delay(2000);
		robot.keyPress(KeyEvent.VK_TAB);
		robot.keyRelease(KeyEvent.VK_TAB);
		robot.keyPress(' ');
		robot.keyRelease(' ');

		// simulate a mouse click over the rightmost button
		robot.delay(2000);
		robot.mouseMove(220, 40);
		robot.mousePress(InputEvent.BUTTON1_MASK);
		robot.mouseRelease(InputEvent.BUTTON1_MASK);

		// capture the screen and show the resulting image
		robot.delay(2000);
		BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300));
		MyImageFrame frame = new MyImageFrame(image);
		frame.setVisible(true);
	}

}

class MyImageFrame extends JFrame {
	public MyImageFrame(Image image) {
		setTitle("Capture");
		setSize(450,350);
		JLabel label=new JLabel(new ImageIcon(image));
		add(label);
	}
}
